package com.ssfeng.netty;

import java.net.InetSocketAddress;
import java.nio.channels.AsynchronousChannelGroup;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Description:
 * @Author: liyuhang
 * @CreateTime: 2021/5/18 3:20 下午
 * @Company:
 */
public class AioSocketServer {

    private ExecutorService executorService;          // Thread pool
    private AsynchronousChannelGroup threadGroup;      // Channel group
    public AsynchronousServerSocketChannel asynServerSocketChannel;  // Server channel
    public void start(Integer port){
        try {
            // 1. Create a cache pool
            executorService = Executors.newCachedThreadPool();
            // 2. Creating Channel Groups
            threadGroup = AsynchronousChannelGroup.withCachedThreadPool(executorService, 1);
            // 3. Create server channels
            asynServerSocketChannel = AsynchronousServerSocketChannel.open(threadGroup);
            // 4. Binding
            asynServerSocketChannel.bind(new InetSocketAddress(port));
            System.out.println("server start , port : " + port);
            // 5. Waiting for Client Request
            asynServerSocketChannel.accept(this, new AioServerHandler());
            // The server is blocked all the time, and the real environment is running under tomcat, so this line of code is not needed.
            Thread.sleep(Integer.MAX_VALUE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        AioSocketServer server = new AioSocketServer();
        server.start(8888);
    }
}
